package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import model.Lesson;

/**
 *
 * @author cska
 *
 */

public class LessonDlgU extends JDialog implements ocDialog {

    protected Lesson lesson;
    protected JButton ok, cancel;
    protected JPanel bp1;
    protected JLabel l1, l2, l3, l4;
    protected JComboBox type;
    protected JTextField theme;
    protected JTextField plan;
    protected JFormattedTextField date;
    public boolean fl;
    protected GridBagConstraints c;

    public LessonDlgU(JFrame owner, boolean modal, String title, Lesson lesson) {
        super(owner, title, modal);
        setLayout(new GridBagLayout());

        this.lesson = lesson;

        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        l1 = new JLabel("Тип:", JLabel.RIGHT);
        add(l1,c);
        c.gridy = 1;
        l2 = new JLabel("Тема:", JLabel.RIGHT);
        add(l2,c);
        c.gridy = 2;
        l3 = new JLabel("План:", JLabel.RIGHT);
        add(l3,c);
        c.gridy = 3;
        l4 = new JLabel("Дата:", JLabel.RIGHT);
        add(l4,c);

        c = new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        String types[] = {"лек.", "лаб.", "прак."};
        type = new JComboBox(types);
        type.setSelectedIndex(lesson.getType());
        add(type,c);
        c.gridy = 1;
        theme = new JTextField();
        theme.setText(lesson.getTheme());
        theme.setHorizontalAlignment(JTextField.CENTER);
        add(theme,c);
        c.gridy = 2;
        plan = new JTextField();
        plan.setText(lesson.getPlan());
        plan.setHorizontalAlignment(JTextField.CENTER);
        add(plan,c);
        c.gridy = 3;
        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("####.##.##");
        } catch (ParseException ex) {
            Logger.getLogger(TeacherWnd.class.getName()).log(Level.SEVERE, null, ex);
        }
        date = new JFormattedTextField(mask);
        date.setHorizontalAlignment(JFormattedTextField.CENTER);
        date.setText(lesson.getDate());
        add(date,c);

        c = new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        bp1= new JPanel(new GridBagLayout());
        add(bp1,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        bp1.add(ok,c);
        c.gridx = 1;
        c.weightx = 0.0;
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        bp1.add(cancel,c);

        setSize(400,220);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-400)/2, (screen.height-220)/2);
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);
    }

    public void ok() {
        fl = true;
        lesson.setType(type.getSelectedIndex());
        lesson.setTheme(theme.getText());
        lesson.setPlan(plan.getText());
        lesson.setDate(date.getText());
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

    public Lesson getLesson() {
        return lesson;
    }

}
